package io.renren.modules.hl.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.fy.entity.FyOrderEntity;
import io.renren.modules.fy.service.FyOrderService;
import io.renren.modules.hl.entity.HlProjectEntity;
import io.renren.modules.hl.service.HlProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.hl.entity.HlNursingRecordEntity;
import io.renren.modules.hl.service.HlNursingRecordService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author tanyajun
 * @email tan82692586@gmail.com
 * @date 2021-05-07 10:58:07
 */
@RestController
@RequestMapping("hl/hlnursingrecord")
public class HlNursingRecordController {
    @Autowired
    private HlNursingRecordService hlNursingRecordService;
    @Autowired
    private HlProjectService hlProjectService;
    @Autowired
    private FyOrderService fyOrderService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("hl:hlnursingrecord:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = hlNursingRecordService.queryPage(params);
        PageUtils page = hlNursingRecordService.selectPageVo(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hl:hlnursingrecord:info")
    public R info(@PathVariable("id") Integer id){
		HlNursingRecordEntity hlNursingRecord = hlNursingRecordService.getById(id);

        return R.ok().put("hlNursingRecord", hlNursingRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hl:hlnursingrecord:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody HlNursingRecordEntity hlNursingRecord){
        boolean isSave = hlNursingRecordService.save(hlNursingRecord);
        if(isSave){
            // 开立护理记录之后生成对应的费用订单entity
            FyOrderEntity fyOrderEntity = new FyOrderEntity();
            // 客户id
            fyOrderEntity.setCustomerId(hlNursingRecord.getCustomerId());
            // 订单数量
            fyOrderEntity.setOrderNum(hlNursingRecord.getNumber());
            // 订单类型
            fyOrderEntity.setOrderType("护理");
            // 根据所选项目id得到项目信息
            HlProjectEntity project = hlProjectService.getById(hlNursingRecord.getProjectId());
            // 设置项目名称
            fyOrderEntity.setOrderName(project.getProjectName());
            // 设置总价
            fyOrderEntity.setOrderCost(hlNursingRecord.getNumber()*project.getProjectPrice());
            fyOrderEntity.setPayState(0);
            // 写入
            boolean save = fyOrderService.save(fyOrderEntity);
            System.out.println(save?"开立护理项目成功":"开立护理项目失败");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hl:hlnursingrecord:update")
    public R update(@RequestBody HlNursingRecordEntity hlNursingRecord){
		hlNursingRecordService.updateById(hlNursingRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hl:hlnursingrecord:delete")
    public R delete(@RequestBody Integer[] ids){
		hlNursingRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
